home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / signatureFactory.js < prev    next >
Text File  |  2010-02-02  |  13KB  |  468 lines

  1. var WisestampSignatureFactory = 
  2. {
  3.     lastInserted: null,
  4.     _prefs: null,
  5.     
  6.     PREFIX: "<!--WISESTAMP_SIG_START-->",
  7.     SUFFIX: "<!--WISESTAMP_SIG_END-->",
  8.     
  9.     //URL_PREFIX: WiseStampComm.HOST + "/link?site=",
  10.     
  11.     get prefs() {
  12.         if (!this._prefs) {
  13.             this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
  14.             getService(Components.interfaces.nsIPrefService);
  15.             this._prefs = this._prefs.getBranch("extensions.wisestamp.");
  16.         }
  17.         return this._prefs;
  18.     },
  19.  
  20.     strings: Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://wisestamp/locale/wisestamp.properties"),
  21.  
  22.     insertSignature: function (aType, doc) 
  23.     {
  24.         WiseStampUtils.log("[signatureFactory.js::insertSignature] >>>>> ");
  25.         
  26.         var win = doc.defaultView;
  27.         var body = doc.body;
  28.         doc.execCommand("inserthtml", false, this.getFullSignatureHTML(aType));
  29.     },
  30.     
  31.     getFullSignatureHTML: function getFullSignatureHTML_name(aType, aData) 
  32.     {
  33.         var sig = this.PREFIX + this.createSignature(aData, aType) + this.SUFFIX;
  34.         return sig;
  35.     },
  36.     
  37.     // Call stack code
  38.     showCallStack: function(){
  39.         var f=this.showCallStack,result="Call stack:\n\n";
  40.         //alert(f.name);
  41.         //alert(f.arguments.callee);
  42.         while((f=f.caller)!==null)
  43.         {
  44.             var match = f.toString().match(/function \w*\(([\w,\s]*)\)/);
  45.             if (match == null)
  46.                 match = f.toString().match(/\w*: function (\w*)/);
  47.  
  48.             if (match == null)
  49.             {
  50.                 alert("match == null: " + f.toString());
  51.                 break;
  52.             }
  53.             
  54.             result += "F:" + match[0] + "\n";
  55.             result += "A:" + this.parseArguments(f.arguments) + "\n";
  56.             result += "\n";
  57.         }
  58.         alert(result);
  59.     },
  60.     
  61.     parseArguments: function(a){
  62.         var result=[];
  63.     
  64.         for(var i=0; i<a.length; i++){
  65.             if ('string' == typeof a[i])
  66.                 result.push("\"" + a[i] + "\"");
  67.             else
  68.                 result.push(a[i]);
  69.         }
  70.     
  71.         return "(" + result.join(", ") + ")";
  72.     },
  73.         
  74.     createSignature: function createSignature_name(data, aType) 
  75.     {
  76.         WiseStampUtils.log("[signatureFactory.js::createSignature] >>>>> ");
  77.         
  78.         if (aType) 
  79.             type = aType;
  80.         else 
  81.             type = data ? "custom" : this.prefs.getCharPref("type");
  82.  
  83.         this.lastInserted = type;
  84.                 
  85.         if (type != "" && type != "none") 
  86.         {
  87.             var sig = new WiseStampSignature(type, data);
  88.             var html = sig.text;
  89.             var str = "<div>" + html + "</div>";
  90.  
  91.             var im = this.getImHtml(sig);
  92.             var services = this.getServicesHtml(sig);
  93.             var advanced = sig.advanced;
  94.             var feed = this.getRssHtml(sig, aType);
  95.  
  96.             if (html == "" && im == "" && services == "" && advanced == "") 
  97.                 return "";
  98.             else 
  99.                 str += im + services;
  100.  
  101.             if (feed != "") 
  102.                 str += "<div style='color: gray; font-size: 13.3px; padding-bottom: 5px;'>" + feed + "</div>";
  103.  
  104.             if (advanced != "") 
  105.                 str += "<div>" + advanced + "</div>";
  106.  
  107.             if (type != "custom." && sig.promote) 
  108.             {
  109.                 var footer = WisestampGetAppObject().getFooter(this.strings);
  110.                 footer = this.wrapLinks(footer);
  111.                 str += footer;
  112.             }
  113.  
  114.             str = "<div dir='" + sig.dir + "'>" + str + "</div>";
  115.             //str = this.wrapLinks(str);
  116.             return "<span style='color: black'>"+str+"</span>";
  117.             
  118.         } else 
  119.             return "";
  120.     },
  121.  
  122.     getRssHtml: function(sig, aType) 
  123.     {
  124.         var rss_title = sig.rss_title;
  125.         var rss_url = sig.rss_url;
  126.         var str = "";
  127.  
  128.         WiseStampUtils.log("[signatureFactory.js::getRssHtml] rss title = " + rss_title + ", rss url = " + rss_url);
  129.         
  130.         if (rss_url != "") 
  131.         {
  132.             if (!this.feedBlog)
  133.             {
  134.                 this.feedBlog = new WiseStampFeedWrapper(new WiseStampRSSFeed(rss_url), new WiseStampPostFormatter());
  135.                 this.rss_refreshed = false;
  136.               }
  137.  
  138.             if (this.feedBlog._feed.url != rss_url)
  139.             {
  140.                 this.feedBlog = new WiseStampFeedWrapper(new WiseStampRSSFeed(rss_url), new WiseStampPostFormatter());
  141.                 this.rss_refreshed = false;
  142.             }
  143.       
  144.               if (!this.rss_refreshed)
  145.             {
  146.                 try {                
  147.                     this.feedBlog.refreshCache(
  148.                         function(){
  149.                             setTimeout(function() {
  150.                                 WisestampSignatureFactory.rss_refreshed = true;
  151.                                 WisestampGetAppObject().updateSiteFromChrome(aType);
  152.                             }, 0);
  153.                         });
  154.                 } 
  155.                 catch (e){
  156.                     return "";
  157.                 }
  158.     
  159.             } else
  160.             {
  161.                 if (this.feedBlog.format())
  162.                 {
  163.                     str += 
  164.                         rss_title + " " +
  165.                         "<span style='color: blue; text-decoration: underline;'>" + 
  166.                         this.getLink(this.feedBlog.getLatestPost().getLink(), this.feedBlog.getLatestPost().getTitle())+
  167.                         "</span>";
  168.                 }
  169.                 
  170.                 this.rss_refreshed = false;
  171.             }
  172.         }
  173.         return str;
  174.     },
  175.  
  176.     getImHtml: function(sig) {
  177.         var ims = sig.im.visible;
  178.         if (ims.length > 0) 
  179.         {
  180.             var imsrow = "";
  181.             var counts = {};
  182.         
  183.             ims.forEach(function(aIM) 
  184.             {
  185.                 var value = "";
  186.                 if (aIM && aIM.length > 0) 
  187.                 {
  188.                     if (aIM in counts) 
  189.                         counts[aIM]++;
  190.                     else 
  191.                         counts[aIM] = 0;
  192.                         
  193.                     if (sig.hasService(aIM, counts[aIM])) 
  194.                         value = sig.getService(aIM, counts[aIM]);
  195.                         
  196.                     if (value.length > 0) 
  197.                     {
  198.                         var name = this.strings.GetStringFromName(aIM);
  199.                         var label = '<font style="color: gray; font-weight: bold; font-size: 9pt;">' + name + ": " + '</font>';
  200.                         var valueText = '<font style="vertical-align: middle;">' + value + '</font>';
  201.                         var alt = "";
  202.                         var src = "http://www.images.wisestamp.com/" + aIM + ".png";
  203.                         var img = '<img src="' + src + '" width="16" height="16" style="vertical-align: middle; padding-right: 5px" alt="{alttext}"/>';
  204.                         var im = "";
  205.                         
  206.                         if (sig.im.showLabels && !sig.im.showIcons) 
  207.                             im = label + value;
  208.                         else if (sig.im.showIcons && !sig.im.showLabels) 
  209.                             im = img.replace("{alttext}", name + ": ") + " " + valueText;
  210.                         else 
  211.                             im = img.replace("{alttext}", "") + label + valueText;
  212.                             
  213.                         if (imsrow != "") 
  214.                             imsrow += "  " + im;
  215.                         else 
  216.                             imsrow = im;
  217.                     }
  218.                 }
  219.             },
  220.             this);
  221.             
  222.             if (imsrow != "") 
  223.             {
  224.                 var align = sig.dir == "rtl" ? "text-align: right;": "";
  225.                 var imprefix = sig.im.title;
  226.                 var prefix = sig.im.hideTitle ? "" : '<span style="color: gray;">' + imprefix + "</span>" + "  ";
  227.             
  228.                 return "<div style='padding: 10px 0 5px 0; font-family: arial,sans-serif;font-size: 13.3px;" + align + "'>" + prefix + imsrow + "</div>";
  229.             }
  230.         }
  231.         return "";
  232.     },
  233.  
  234.     getServicesHtml: function(sig) 
  235.     {
  236.         var services = sig.services.visible;
  237.         if (services.length > 0) 
  238.         {
  239.             var servicerow = "", counts = {};
  240.             
  241.             services.forEach(function(service) 
  242.             {
  243.                 if (service && service.length > 0) 
  244.                 {
  245.                     var value = "";
  246.                     if (service in counts) 
  247.                         counts[service]++;
  248.                     else 
  249.                         counts[service] = 0;
  250.                         
  251.                     if (sig.hasService(service, counts[service])) 
  252.                         value = sig.getService(service, counts[service]);
  253.                         
  254.                     if (value.length > 0) 
  255.                     {
  256.                         var url = value, imghtml = "", icon, alt = "", style = "";
  257.                         var serviceName = this.strings.GetStringFromName(service);
  258.  
  259.                         if (!sig.services.showLabels) 
  260.                         {
  261.                             alt = ' alt="' + serviceName + '" ';
  262.                             style = ' style="vertical-align: middle; padding-bottom: 5px;" ';
  263.                         
  264.                         } else 
  265.                             style = ' style="padding-right: 3px; padding-bottom: 5px; vertical-align: middle" ';
  266.                         
  267.                         if (sig.services.showIcons) 
  268.                         {
  269.                             icon = "http://www.images.wisestamp.com/" + service + ".png";
  270.                             imghtml += '<img src="' + icon + '" ' + alt + style + ' border="0" width="16" height="16"/>';
  271.                         }
  272.                         
  273.                         if (sig.services.showLabels)
  274.                             imghtml += "<span style='color: blue; font-size: 10pt; text-decoration: underline;'>" + serviceName + "</span>";
  275.                             
  276.                         //var html = '<a style="padding: 0 2px 0 2px;" href="' + url + '" target="_service" style="color: blue; font-size: 10pt">' + imghtml + '</a>';
  277.                         
  278.                         var html = this.getLink(url,imghtml,
  279.                             'style="padding: 0 2px 0 2px; color: blue; font-size: 10pt; target="_service"'); 
  280.  
  281.                         servicerow += html;
  282.                     }
  283.                 }
  284.             },
  285.             this);
  286.             
  287.             if (servicerow != "") 
  288.             {            
  289.                 var align = sig.dir == "rtl" ? "text-align: right;": "";
  290.                 var serviceprefix = sig.services.title;
  291.                 var prefix = sig.services.hideTitle ? "" : '<span style="color: gray;">' + serviceprefix + "</span>" + "  ";
  292.                 return "<div style='padding: 5px 0 5px 0; font-family: arial,sans-serif;font-size: 13.3px;" + align + "'>" + prefix + servicerow + "</div>";
  293.             }
  294.         }
  295.         return "";
  296.     },
  297.     
  298.     getLink: function(url,text,params)
  299.     {
  300.         if (WiseStampComm.loadSettings('wrap_links') == 1)
  301.         {
  302.             if (url.slice(0,7) == 'http://')
  303.                 url = url.slice(7);
  304.                 
  305.             url = WiseStampComm.HOST + "/link?u=" + WiseStampUtils.getUserCode() + "&site=" + url;
  306.         }
  307.             
  308.         return '<a href="'+url+'" '+params+'>'+text+'</a>';
  309.     },
  310.     
  311.     wrapLinks: function(html)
  312.     {
  313.         var prefix = WiseStampComm.HOST + "/link?u=" + WiseStampUtils.getUserCode() + "&site=";
  314.         return html.replace(/(href="http:\/\/)/g,"href=\""+prefix);
  315.         //html = html.replace(/href="http://(.+?)\"/,"http://$1\"");
  316.     }    
  317. }
  318.  
  319. function WiseStampSignature(type, aData) 
  320. {
  321.     this.type = this.im.type = this.services.type = type;
  322.     
  323.     if (type == "custom") 
  324.     {
  325.         this.data = this.im.data = this.services.data = aData.data;
  326.         this.settings = this.im.settings = this.services.settings = aData.settings;
  327.     
  328.     } else 
  329.     {
  330.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  331.         this.prefs = prefs.getBranch("extensions.wisestamp.");
  332.         this.branch = prefs.getBranch("extensions.wisestamp." + type + ".");
  333.         this.im.branch = prefs.getBranch("extensions.wisestamp." + type + ".im");
  334.         this.services.branch = prefs.getBranch("extensions.wisestamp." + type + ".services");
  335.     }
  336. }
  337.  
  338. WiseStampSignature.prototype = {
  339.     branch: null,
  340.     
  341.     get text() {
  342.         if (this.branch) 
  343.             return this.branch.getComplexValue("text", Components.interfaces.nsISupportsString).data;
  344.         else     
  345.             return this.data.text;
  346.     },
  347.     
  348.     get dir() {
  349.         if (this.branch) 
  350.             return this.branch.getCharPref("dir");
  351.         else 
  352.             return this.data.dir;
  353.     },
  354.     
  355.     get advanced() {
  356.         if (this.branch) 
  357.             return this.branch.getComplexValue("advanced", Components.interfaces.nsISupportsString).data;
  358.         else 
  359.             return this.data.advanced;
  360.     },
  361.     
  362.     get rss_title() {
  363.         if (this.branch)
  364.             return this.branch.getComplexValue("rss-title", Components.interfaces.nsISupportsString).data;
  365.         else
  366.             return this.data.rsstitle;
  367.     },
  368.     
  369.     get rss_url() {
  370.         if (this.branch)
  371.             return this.branch.getComplexValue("rss-url", Components.interfaces.nsISupportsString).data;
  372.         else
  373.             return this.data.rssurl;
  374.     },
  375.     
  376.     im: {
  377.         get showLabels() {
  378.             if (this.branch)
  379.                 return WiseStampPrefs.getBoolPref(this.type+".im.showLabels",true,"im.text");
  380.             else
  381.                 return this.data.imFormat.showLabels;
  382.         },
  383.         get showIcons() {
  384.             if (this.branch)
  385.                 return WiseStampPrefs.getBoolPref(this.type+".im.showIcons",true,"im.icons");
  386.             else
  387.                 return this.data.imFormat.showIcons;
  388.         },
  389.         get hideTitle() {
  390.             if (this.branch)
  391.                 return WiseStampPrefs.getBoolPref(this.type+".im.hideTitle",false,"hidechat");
  392.             else
  393.                 return this.data.imFormat.hideTitle;
  394.         },
  395.         get title() {
  396.             if (this.branch)
  397.                 return WiseStampPrefs.getComplexPref(this.type+".im.title",null,"chat");
  398.             else
  399.                 return this.data.imFormat.title;
  400.         },
  401.         get visible() {
  402.             if (this.branch) 
  403.                 return this.branch.getCharPref("").split(",");
  404.             else 
  405.                 return this.data.show.im.split(",");
  406.         }
  407.     },
  408.     
  409.     services: {
  410.         get showLabels() {
  411.             if (this.branch)
  412.                 return WiseStampPrefs.getBoolPref(this.type+".services.showLabels",false,"services.text");
  413.             else
  414.                 return this.data.servicesFormat.showLabels;
  415.         },
  416.         get showIcons() {
  417.             if (this.branch)
  418.                 return WiseStampPrefs.getBoolPref(this.type+".services.showIcons",true,"services.icons");
  419.             else
  420.                 return this.data.servicesFormat.showIcons;
  421.         },
  422.         get hideTitle() {
  423.             if (this.branch)
  424.                 return WiseStampPrefs.getBoolPref(this.type+".services.hideTitle",false,"hidecontactme");
  425.             else
  426.                 return this.data.servicesFormat.hideTitle;
  427.         },
  428.         get title() {
  429.             if (this.branch)
  430.                 return WiseStampPrefs.getComplexPref(this.type+".services.title",null,"contactme");
  431.             else
  432.                 return this.data.servicesFormat.title;
  433.         },
  434.         get visible() {
  435.             if (this.branch) 
  436.                 return this.branch.getCharPref("").split(",");
  437.             else 
  438.                 return this.data.show.services.split(",");
  439.         }
  440.     },
  441.     
  442.     hasService: function(aData) {
  443.         if (this.branch) 
  444.             return this.branch.prefHasUserValue(aData);
  445.         else 
  446.             return ((aData in this.data.im && this.data.im[aData].length > 0) || (aData in this.data.services && this.data.services[aData].length > 0))
  447.     },
  448.     
  449.     getService: function(aData, count) 
  450.     {
  451.         if (this.branch) 
  452.             return this.branch.getCharPref(aData).split("\r\n")[count];
  453.         else if (aData in this.data.im) 
  454.             return this.data.im[aData][count];
  455.         else if (aData in this.data.services) 
  456.             return this.data.services[aData][count];
  457.         else 
  458.             return null;
  459.     },
  460.     
  461.     get promote() {
  462.         if (this.branch)
  463.             return this.prefs.getBoolPref("promote");
  464.         else 
  465.             return this.settings.promote;
  466.     }
  467. }
  468.